import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-damagechannel',
	templateUrl: './damageChannel.component.html',
	styleUrls: ['./damageChannel.component.css']
})
export class DamageChannelComponent implements OnInit {
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	carList: any[] = [];
	selectedCompanyId: string = '';
	selectedCompanyName: string = '';
	deviceId: string = '';//设备ID
	carNumber: string = ''; //车牌号码
	startTime: string = '';
	endTime: string = '';

	carTypeList: any = [];
	carUseList: any = {};
	carDeatailUseList: any = {};

	activeSelected: string = '';
	startTime_chart: string = '';
	endTime_chart: string = '';
	isChartView: boolean = false;
	//地图折线组件所需参数 start
	selectedRow: any;//折线子组件获取数据的参数
	drawTrackArr: any;//折线子组件传回   地图子组件画轨迹的数据
	isMapView = false;//折线图子组件选中是否展开地图

	type = '3';		//时间类型
	sensorType = '';	//传感器类型
	isMatch = '';		//是否匹配
	filterType = '1';	//数据过滤

	specifyId = {
		picker1: 'datetimepicker3',
		picker2: 'datetimepicker4'
	}
	specifyTime = {
		startTime: '',
		endTime: '',
	}
	editRowParams: any = {
		id: '',
		carNumber: '',
		bridgeDate: '',
		type: '1',
		isDetail: '0',
		remark: '',
	};


	//地图折线组件所需参数 end
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute,) {
		activatedRoute.queryParams.subscribe(queryParams => {
			if (queryParams.nowPage) {
				this.curPage = queryParams.nowPage;
			}
		});
	}
	//请求传感器类型的接口
	querySensor() {
		this.jsonApiService.http('sensorManage/querySensorList.html', '').subscribe(data => {
			if (data.success) {
				this.inputParam = data.data
			}
		})
	}
	inputParam: any = [];
	//组件传回来 传感器类型
	sensorSelected(data) {
		this.sensorType = data.sensorType;
	}
	//获取选中的公司与车辆号码
	getSelectTableTime(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
	getSelectChartTime(time) {
		this.startTime_chart = time.startTime;
		this.endTime_chart = time.endTime;
	}
	//地图折线组件方法 start
	runParent(value) {
		// 接收子组件传递过来的数据 
		this.drawTrackArr = value;
	}
	showMapView(val) {
		this.isMapView = val;
	}
	drawAbnormalChart() {
		if (!this.selectedRow) {
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		if (!this.startTime_chart || !this.endTime_chart) {
			this.jsonApiService.message('起始时间不能为空');
			return;
		}
		let timeCheck = common.correctTimeParams(this.startTime_chart, this.endTime_chart, 0);
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let params = {
			deviceId: this.selectedRow.deviceId,
			startTime: this.startTime_chart,
			endTime: this.endTime_chart,
		}
		// this.selectedRow=params;
		this.lineParamDeal(params);
	}
	checkAbnormal(row) {
		this.activeSelected = row.id;
		// this.startTime_chart=common.getTimeMmss( new Date(new Date(row.startTimeByAuto).getTime()-5*60*60*1000) );
		// this.endTime_chart=common.getTimeMmss( new Date(new Date(row.endTimeByAuto).getTime()+5*60*60*1000) );
		if (row.startTimeByAuto || row.endTimeByAuto) {		//算法开始时间结束时间存在就用算法开始结束世家你
			this.startTime_chart = row.startTimeByAuto;
			this.endTime_chart = row.endTimeByAuto;
		}
		else {
			this.startTime_chart = row.startTimeByHand;
			if (row.endTimeByHand) {			//如果结束时间人工存在就赋值
				this.endTime_chart = row.endTimeByHand;
			} else {							//结束时间人工不存在，则获取开始时间人工，往后推七天，赋值时间
				const time = (new Date(row.startTimeByHand).getTime() + 7 * (60 * 60 * 24 * 1000));
				if (new Date() < new Date(time)) {		//如果往后推的时间大于当前时间，则取当前时间
					this.endTime_chart = common.getNowTime();
				} else {
					this.endTime_chart = common.getTimeMmss(new Date(time));
				}
			}
		}
		this.isChartView = true;
		// row.startTime=this.startTime_chart;
		// row.endTime=this.endTime_chart;
		this.specifyTime = {
			startTime: this.startTime_chart,
			endTime: this.endTime_chart,
		}
		// this.selectedRow=row;
		this.lineParamDeal(row);
	}

	//获取传感器通道参数
	lineParamDeal(row) {
		let reqdata = 'deviceId=' + row.deviceId;
		this.jsonApiService.http("realtime/getRealTimeData.html", reqdata).subscribe(result => {
			if (result.success && result.data) {
				// row.coefficient = result.data.coef;
				// this.selectedRow = row;
				let params = {
					deviceId: row.deviceId,
					startTime: this.startTime_chart,
					endTime: this.endTime_chart,
					coefficient: result.data.coef,
				}
				this.selectedRow = params;
			}
		})
	}

	del_chartDate() {
		this.startTime_chart = '';
		this.endTime_chart = '';
	}
	closeChart() {
		this.isChartView = false;
	}
	ngOnInit() {
		if (this.inputParam.length == 0) { this.querySensor(); }// 请求传感器列表接口
		this.load();
	}
	//  初始化加载list数据
	load() {
		let timeCheck = common.correctTimeParams(this.startTime, this.endTime, 0);
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				startTime: this.startTime,
				endTime: this.endTime,
				searchKey: this.searchKey,
				deviceId: this.deviceId,
				companyId: this.selectedCompanyId,
				companyName: this.selectedCompanyName,
				sensorType: this.sensorType,
				type: this.type,		//时间类型
				isMatch: this.isMatch,
				filterType: this.filterType
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("sensormonitor/damageSensorForPage.html", reqdata).subscribe(result => {
			if (!result.isSuccess) {
				this.jsonApiService.message(result.message);
				return;
			}
			this.tableData = result.exhibitDatas;
			this.totalCount = result.recordCount;
			this.tableData.map(item => {
				if (item.currentCoef) {
					let currentCoef = [];
					for (let i = 0; i < item.currentCoef.length; i++) {
						let _item = item.currentCoef[i];
						if (_item) {
							let obj: any = {
								value: item.currentCoef[i],
								index: i + 1
							}
							currentCoef.push(obj);
						}
					}
					item.currentCoefT = currentCoef;
				} else {
					item.currentCoefT = [];
				}
			});
		})

	}

	ngOnDestroy() {
		document.onclick = null;
	}
	//刷新
	refresh() {
		this.load();
	}

	//查询
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}

	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}

	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
	// 导出
	dataExceport() {
		var parameters = {
			startTime: this.startTime,
			endTime: this.endTime,
			searchKey: this.searchKey,
			deviceId: this.deviceId,
			companyId: this.selectedCompanyId,
			companyName: this.selectedCompanyName,
			sensorType: this.sensorType,
			type: this.type,		//时间类型
			isMatch: this.isMatch,
			filterType: this.filterType
		};
		let timeCheck = common.correctTime(parameters, ['type', 'filterType'])
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'sensormonitor/exportDamageSensorMatchList.html')
	}
}
